// pages/courses/coursesxiangqin/coursesxiangqin.js
let app=getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    url:'',
    course:'',
    questions:'',
    ratings:'',
    levelId:0,
    level:'',
    consultantId:0,
    tags:'',
    userId:0,
    // courseId:0,
    sc:false,
    index:0
  },
  db(){
    wx.showToast({
      title: '购买成功',
    }) 
  },
  changeIndex(e){
    this.setData({
      // 动态获取从data-index获取的参数
      index:e.currentTarget.dataset.index
    })
  },
  quxiao(){
    if(this.data.userId==0||this.data.userId==null){
      wx.showModal({
        title: '未登录',
        content: '前往登录',
        complete: (res) => {
          if (res.cancel) {
            
          }     
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login',
            })
          }
        }
      })
      return;
    }
    wx.request({
      url:`${this.data.url}courseCollect/delete?userId=${this.data.userId}&courseId=${this.data.id}`,
      method:'DELETE',
      success:(resp)=>{
    this.setData({
          sc:false
        })
        wx.showToast({
          title: '取消收藏',
          icon:'none'
        })
      },
    })
  },
  shouchang(){
    if(this.data.userId==0||this.data.userId==null){
      wx.showModal({
        title: '未登录',
        content: '前往登录',
        complete: (res) => {
          if (res.cancel) {
            
          }
      
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/login/login',
            })
          }
        }
      })
      return;
    }
    console.log(this.data.id);
   
    wx.request({
      url: this.data.url+'courseCollect/collect',
      method:'POST',
      data:{
        userId:this.data.userId,
        courseId:this.data.id
      },
      
      success:(resp)=>{
        if (resp.data.code!=200) {
          wx.showToast({
            title: '该课程已收藏',
            icon:"none"
          })
          return;
        }
//  console.log(12313)
    this.setData({
          sc:true
        })
        wx.showToast({
          title: '收藏成功',
        })
      },
      fail: (err) => {
        // 网络错误、域名错误、超时等
        console.error('请求失败', err);
        wx.showToast({ title: '网络异常，请稍后重试', icon: 'none' });
      },
    
    })
  },
  getCourse(){
    wx.request({
      url: this.data.url+'course/'+this.data.id,
      method:'GET',
      success:(resp)=>{
        console.log(resp.data.data)
        this.setData({
          course:resp.data.data,
          levelId:resp.data.data.consultant.consultantLevelId,
          consultantId:resp.data.data.consultantId,
          // courseId:resp.data.data.id
        },()=>{
           {
            console.log(this.data.levelId+"咨询师id")
            wx.request({
              url: this.data.url+'Level/'+this.data.levelId,
              method:'GET',
              success:(resp)=>{
                console.log(resp.data.data)
                this.setData({
                  level:resp.data.data
                })
              }
            })
          }
          {
         
            wx.request({
              url: this.data.url+'Consultanttag/'+this.data.consultantId,
              method:'GET',
              success:(resp)=>{
                // console.log('sssssss')
                console.log(resp.data.data)
                this.setData({
                  tags:resp.data.data
                })
              }
            })
          }
        })
      }
    })
  },
  getClassQuestion(){
    wx.request({
      url: this.data.url+'classQuestion/all',
      method:'GET',
      success:(resp)=>{
        console.log(resp.data.data)
        this.setData({
          questions:resp.data.data
        })
      }
    })
  },
  getCourseRating(){
    wx.request({
      url: this.data.url+'courseRating/'+this.data.id,
      method:'GET',
      success:(resp)=>{
        console.log(resp.data.data)
        this.setData({
          ratings:resp.data.data
        })
      }
    })
  },
  checkIsCollected(){
    wx.request({
      url: this.data.url+'courseCollect/check',
      data:{
        userId:this.data.userId,
        courseId:this.data.id
      },
      success:(resp)=>{
        console.log(resp.data.data);
        if (resp.data.data==null) {
          this.setData({
            sc:false
          })
        } else{
          this.setData({
            sc:true
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      sc:false
    })
    const user=wx.getStorageSync('user')
    this.setData({
      userId:user.id
    })
    console.log(options.id)
    this.setData({
      id:options.id
    })
    this.setData({
      url:app.globalData.url
    })
    this.getCourse()  //课程信息
    
    
    this.checkIsCollected()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getCourseRating()  //课程评价
    this.getClassQuestion() //课堂交流
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})